package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.transportline.TransportLineTypeDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.entity.transportline.PdTransportLineType;
import com.itheima.pinda.mapper.PdTransportLineTypeMapper;
import com.itheima.pinda.mapstruct.TransportDTO2Entity;
import com.itheima.pinda.service.PdTransportLineTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PdTransportLineTypeServiceImpl extends ServiceImpl<PdTransportLineTypeMapper, PdTransportLineType> implements PdTransportLineTypeService {

    @Resource
    private TransportDTO2Entity dto2Entity;


    /**
     * 添加线路类型
     *
     * @param dto 线路类型信息
     * @return 线路类型信息
     */
    @Override
    public TransportLineTypeDto saveTransportLineType(TransportLineTypeDto dto) {
        PdTransportLineType pdTransportLineType = dto2Entity.conver(dto);
        super.save(pdTransportLineType);
        dto = dto2Entity.conver(pdTransportLineType);
        return dto;
    }


    /**
     * 获取线路类型分页数据
     *
     * @param page       页码
     * @param pageSize   页尺寸
     * @param typeNumber 类型编号
     * @param name       类型名称
     * @param agencyType 机构类型
     * @return 线路类型分页数据
     */
    @Override
    public PageResponse<TransportLineTypeDto> findByPage(Integer page, Integer pageSize,
                                                         String typeNumber, String name, Integer agencyType) {
        Page<PdTransportLineType> transportLineTypePage = super.page(new Page<>(page, pageSize), Wrappers.<PdTransportLineType>lambdaQuery()
                .like(StringUtils.isNotBlank(name), PdTransportLineType::getName, name)
                .like(StringUtils.isNotBlank(typeNumber), PdTransportLineType::getTypeNumber, typeNumber)
                .and(Objects.nonNull(agencyType),
                        qw -> qw.eq(PdTransportLineType::getStartAgencyType, agencyType)
                                .or()
                                .eq(PdTransportLineType::getEndAgencyType, agencyType))
                .eq(PdTransportLineType::getStatus, Constant.DATA_DEFAULT_STATUS)
                .orderByAsc(PdTransportLineType::getId));

        return PageResponse.<TransportLineTypeDto>builder()
                .page(page)
                .pagesize(pageSize)
                .pages(transportLineTypePage.getPages())
                .counts(transportLineTypePage.getTotal())
                .items(transportLineTypePage.getRecords()
                        .stream()
                        .map(item -> dto2Entity.conver(item))
                        .collect(Collectors.toList()))
                .build();
    }

    /**
     * 获取线路类型列表
     *
     * @param ids 线路类型id列表
     * @return 线路类型列表
     */
    @Override
    public List<TransportLineTypeDto> findAll(List<String> ids) {
        List<PdTransportLineType> pdTransportLineTypeList = super.list(Wrappers.<PdTransportLineType>lambdaQuery()
                .in(ids != null && ids.size() > 0, PdTransportLineType::getId, ids)
                .eq(PdTransportLineType::getStatus, Constant.DATA_DEFAULT_STATUS)
                .orderByAsc(PdTransportLineType::getId));

        return pdTransportLineTypeList.stream()
                .map(item -> dto2Entity.conver(item))
                .collect(Collectors.toList());
    }
}
